﻿using System;
using ETModel;
using UnityEngine;

namespace ETHotfix {
    [ObjectSystem]
    public class OperaComponentAwakeSystem : AwakeSystem<OperaComponent> {
        public override void Awake(OperaComponent self) {
            self.Awake();
        }
    }

    [ObjectSystem]
    public class OperaComponentUpdateSystem : UpdateSystem<OperaComponent> {
        public override void Update(OperaComponent self) {
            self.Update();
        }
    }

    public class OperaComponent : Component {

        private Unit _operaUnit;
        private AnimatorComponent _opearAni;
        private TurnComponent _turnComp;

        public Vector3 ClickPoint;
        public Unit OperaUnit {
            get {
                return _operaUnit;
            }
            set {
                _operaUnit = value;
                _opearAni = _operaUnit.GetComponent<AnimatorComponent>();
                _turnComp= _operaUnit.GetComponent<TurnComponent>();
            }
        }

        public int mapMask;

        public void Awake() {
            this.mapMask = LayerMask.GetMask("Map");
        }

        private readonly Frame_ClickMap frameClickMap = new Frame_ClickMap();

        public void Update() {
            if (OperaUnit == null || OperaUnit.IsDisposed || _opearAni == null) return;

            if (Input.GetMouseButtonDown(1) && !_opearAni.Animator.GetCurrentAnimatorStateInfo(0).IsName("Attack")) {
                //右键点地面移动, 攻击状态下不能移动
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, 1000, this.mapMask)) {
                    this.ClickPoint = hit.point;
                    frameClickMap.X = this.ClickPoint.x;
                    frameClickMap.Y = this.ClickPoint.y;
                    frameClickMap.Z = this.ClickPoint.z;
                    SessionComponent.Instance.Session.Send(frameClickMap);
                }
            }
        }
    }
}
